package org.dtrd.modules.doctor.controller;

import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.doctor.entity.bean.PatientServiceInfo;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctor;
import org.dtrd.modules.doctor.entity.request.DoctorServiceRequest;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorService;
import org.dtrd.modules.doctor.service.IDtrdRlDoctorTeamDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author 51465
 */
@Api(tags = "医生服务")
@RestController
@Slf4j
@RequestMapping("/doctor")
public class DoctorServiceController extends BaseController {

    @Autowired
    private IDtrdEntRdDoctorService doctorService;
    @Autowired
    private BaseCommonService baseCommonService;

    @Autowired
    private IDtrdRlDoctorTeamDoctorService doctorTeamDoctorService;

    @ApiOperation(value = "医生界面服务查询", response = PatientServiceInfo.class)
    @PostMapping("/service")
    public Result<?> getDoctorService(@RequestBody @Validated DoctorServiceRequest request) {
        //判断参数有效性
        Integer doctorId = request.getDoctorId();
        // 增加 团队查询逻辑 肖 2022-07-20 15:40:08
        // List<Integer> teamDoctorIdList = doctorTeamDoctorService.getTeamDoctorIdList(doctorId);
        // if (doctorId != null && CollectionUtil.isEmpty(teamDoctorIdList)) {
        //     if (teamDoctorIdList == null) {
        //         teamDoctorIdList = new ArrayList<>();
        //     }
        //     teamDoctorIdList.add(doctorId);
        // }

        Integer teamId = Optional.ofNullable(doctorId).map(id -> doctorService.getById(id)).map(DtrdEntRdDoctor::getTeamId).orElse(null);
        List<Integer> teamDoctorIdList;
        log.info("teamId+++++++++++++++"+teamId);
        if (teamId != null) {
            teamDoctorIdList = doctorTeamDoctorService.getTeamDoctorIdListByTeamId(teamId);
        } else {
            teamDoctorIdList = doctorTeamDoctorService.getTeamDoctorIdList(doctorId);
        }

        if (doctorId != null && CollectionUtil.isEmpty(teamDoctorIdList)) {
            if (teamDoctorIdList == null) {
                teamDoctorIdList = new ArrayList<>();
            }
            teamDoctorIdList.add(doctorId);
        }

        Integer packageId = request.getPackageId();
        List<PatientServiceInfo> doctorReverseService = doctorService.getDoctorService(teamDoctorIdList, packageId);
        String logContent = String.format("获取医生服务页患者信息, %d", doctorId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(doctorReverseService);
    }
}
